;chez-scheme 无穷流定义就死循环了，只好按chez 文档修改无穷流实现方式。

(define stream-car 
    (lambda (s) (car (force s)))
)
(define stream-cdr 
    (lambda (s) (cdr (force s)))
)


(define (stream-ref s n)
    (if (= n 0)
        (stream-car s)
        (stream-ref (stream-cdr s) (- n 1))
    )
)

(define counters
    (let next ([n 1])
        (delay (cons n (next (+ n 1))))
    )
)

(define (stream-add s1 s2)
    (delay 
        (cons 
            (+ (stream-car s1) (stream-car s2))
            (stream-add (stream-cdr s1) (stream-cdr s2))
        )
    )
)

(define (mul-streams s1 s2)
    (delay 
        (cons 
            (* (stream-car s1) (stream-car s2))
            (mul-streams (stream-cdr s1) (stream-cdr s2))
        )
    )
)


(define factorials 
    (delay 
        (cons 1 (mul-streams counters counters))
    )
)

(display (stream-ref factorials 3))
(exit)